/*
 * plat_lj.cpp
 *
 *  Created on: Aug 21, 2014
 *      Author: killerg
 */


#include "bill_deps.h"
#include "bill.h"
#include "bill_cfg.h"


static void send_res(http_server::p_request req, const string &res) {
	req->send_reply(200, res.c_str());
	if (res != "success") {
		UTIL_LOG(E, "lj bill query:\n%s\nresult:%s\n", evhttp_request_get_uri(req->req), res.c_str());
	}
}

bool init_lj() {
    g_https->handle("/bill/lj", [](http_server::p_request req){
    	if (g_svrmgr->db_set >= 100) {
    		send_res(req, "trylater");
        	return;
    	}

#define CHECK_ARG(name)	\
    	string a_##name = req->query[#name];	\
    	if (a_##name.empty() || a_##name == "") {	\
    		send_res(req, "need "#name);	\
    		return;	\
    	}

    	CHECK_ARG(orderId);
    	CHECK_ARG(price);
    	CHECK_ARG(channelCode);
    	CHECK_ARG(callbackInfo);
    	CHECK_ARG(sign);



        std::vector<std::string> vNote;
        string decoded = decode(a_callbackInfo.c_str());
        string decodedcallbackInfo = base64_decode(decoded.c_str(), decoded.length());
        split(decodedcallbackInfo, ",", vNote);
		if (vNote.size() != 3) {
    		send_res(req, "callbackInfo err");
            return;
		}

    	std::string toSign = fmt_cstr("%s%s%s%s",
									a_orderId.c_str(),
									a_price.c_str(),
									a_callbackInfo.c_str(),
									"8b245577af854f1ab5274807cf4b2e17");

    	if (Md5(toSign.c_str()) != a_sign) {
    		send_res(req, "sign err");
    		return;
    	}

        string account = fmt_cstr("%s-%s", a_channelCode.c_str(), vNote[2].c_str());
        string orderId = fmt_cstr("%s-%s", a_channelCode.c_str(), a_orderId.c_str());
        double payFee = atoi(a_price.c_str()) * g_cfg->Rate / 100.0f;
        int serverid = atoi(vNote[0].c_str());
        string machinecode = vNote[1];


        if (account.empty() || orderId.empty() || payFee <= 0 || serverid < 0) {
        	send_res(req, "invalid arg");
            return;
        }

        auto playerinfo = g_svrmgr->get_player_info(serverid, account);
        if (NULL == playerinfo) {
        	if (g_cfg->trace)
        		UTIL_LOG(E, "lj bill NO PLAYER INFO\n serverid:%d account:%s\n", serverid, account.c_str());
        	send_res(req, "no player info");
            return;
        }

        g_svrmgr->post_order(serverid, orderId, playerinfo->playerid, payFee, machinecode, [req](bool ok){
            if (ok)
            	send_res(req, "success");
            else
            	send_res(req, "err post order");
        });
    });

    return true;
}



bool init_au() {
    g_https->handle("/bill/au", [](http_server::p_request req){
    	if (g_svrmgr->db_set >= 100) {
    		send_res(req, "trylater");
        	return;
    	}

#define CHECK_ARG(name)	\
    	string a_##name = req->query[#name];	\
    	if (a_##name.empty() || a_##name == "") {	\
    		send_res(req, "need "#name);	\
    		return;	\
    	}

    	CHECK_ARG(account);
    	CHECK_ARG(orderid);
    	CHECK_ARG(payfee);
    	CHECK_ARG(serverid);

        string account = fmt_cstr("au-%s", a_account.c_str());
        string orderId = fmt_cstr("au-%s", a_orderid.c_str());
        double payFee = atoi(a_payfee.c_str()) * g_cfg->Rate / 100.0f;
        int serverid = atoi(a_serverid.c_str());
        string machinecode = "";

        if (account.empty() || orderId.empty() || payFee <= 0 || serverid < 0) {
        	send_res(req, "invalid arg");
            return;
        }

        auto playerinfo = g_svrmgr->get_player_info(serverid, account);
        if (NULL == playerinfo) {
        	if (g_cfg->trace)
        		UTIL_LOG(E, "lj bill NO PLAYER INFO\n serverid:%d account:%s\n", serverid, account.c_str());
        	send_res(req, "no player info");
            return;
        }

        g_svrmgr->post_order(serverid, orderId, playerinfo->playerid, payFee, machinecode, [req](bool ok){
            if (ok)
            	send_res(req, "success");
            else
            	send_res(req, "err post order");
        });
    });

    return true;
}
